package webrtc

import (
	"github.com/gorilla/websocket"
	"github.com/pion/webrtc/v4"
	"net/http"
	"sync"
)

var upgrader = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

// 定义PeerConnection结构体
type PeerConnection struct {
	pc             *webrtc.PeerConnection
	mu             sync.Mutex
	onTrackHandler func(*webrtc.TrackRemote, *webrtc.RTPReceiver)
}

func (pc *PeerConnection) OnTrack(f func(*webrtc.TrackRemote, *webrtc.RTPReceiver)) {
	pc.mu.Lock()
	defer pc.mu.Unlock()
	pc.onTrackHandler = f
}

// 设置远程连接细腻系
func (pc *PeerConnection) SetRemoteDescription(desc webrtc.SessionDescription) error {
	return pc.pc.SetRemoteDescription(desc)
}

func (pc *PeerConnection) CreateAnswer(options *webrtc.AnswerOptions) (webrtc.SessionDescription, error) {
	return pc.pc.CreateAnswer(options)
}

func (pc *PeerConnection) SetLocalDescription(desc webrtc.SessionDescription) error {
	return pc.pc.SetLocalDescription(desc)
}

func (pc *PeerConnection) AddICECandidate(candidate webrtc.ICECandidateInit) error {
	return pc.pc.AddICECandidate(candidate)
}

func (pc *PeerConnection) OnICECandidate(f func(*webrtc.ICECandidate)) {
	pc.pc.OnICECandidate(f)
}
